#include "StdAfx.h"
#include "RTPPacket.h"


RTPPacket::RTPPacket( WORD size )
{
	ASSERT( size > RTP_HEAD_LEN );
	buffer = new BYTE[ size ];
	bufferSize = size;

	payload = buffer + RTP_HEAD_LEN;
	payloadSize = bufferSize - RTP_HEAD_LEN;
	SetVersion( RTP_VERSION_CONST);
	SetPayLoadType( RTP_PAYLOADTYPE_CONST);
	SetSSRC( RTP_SSRC_CONST);
}

RTPPacket::~RTPPacket(void)
{
	if( buffer != NULL ) {
		delete[] buffer;
	}

}



int RTPPacket::SetPayLoad(const BYTE * pData,DWORD size ) {
	ASSERT ( size == payloadSize );
	memcpy( payload ,pData, size );
	return 0;
}


BYTE	RTPPacket::GetVersion() {
	return buffer[RTP_VERSION_OFFSET];
}

int		RTPPacket::SetVersion(BYTE version) {
	buffer[RTP_VERSION_OFFSET] = version ;
	return 0;
}

BYTE  RTPPacket::GetPayLoadType() {
	return	buffer[RTP_PAYLOADTYPE_OFFSET];
}


int  RTPPacket::SetPayLoadType(BYTE payLoadType ) {
	buffer[RTP_PAYLOADTYPE_OFFSET ] =  payLoadType  ;
	return 0;
}

WORD RTPPacket::GetSeqNum () {
	return ntohs( *(WORD*)(buffer+RTP_SEQ_NUM_OFFSET )  );
}

int		RTPPacket::SetSeqNum(WORD seqNum) {
	*(WORD*)(buffer+RTP_SEQ_NUM_OFFSET) = htons(seqNum);
	return 0;
}

DWORD  RTPPacket::GetTimeStamp( ) {
	return  ntohl( *( DWORD* )( buffer+RTP_TIME_STAMP_OFFSET ) );
}

int RTPPacket::SetTimeStamp( DWORD timeStamp ) {
	*(DWORD*)(buffer+RTP_TIME_STAMP_OFFSET) = htonl(timeStamp);
	return 0;
}


DWORD  RTPPacket::GetSSRC()  {
	return  ntohl( *( DWORD* )( buffer+RTP_SSRC_OFFSET ) );
}

int RTPPacket::SetSSRC( DWORD ssrc ) {
	*(DWORD*)(buffer+RTP_SSRC_OFFSET) = htonl(ssrc);
	return 0;
}

BOOL RTPPacket::SetFirstFlag(void)
{
	payload[0]|=0x80;
	return TRUE;
}
#ifdef  DEBUG

//extern int printf(LPCTSTR pszFormat, ...);


int RTPPacket::Test() {				// for Debug
	RTPPacket packet( 50 );
	packet.SetVersion( 20 );
	packet.SetPayLoadType( 96);
	packet.SetTimeStamp( TIMESCALE );
	BYTE pay[50-12];
	packet.SetPayLoad(pay, 50-12);
	packet.PacketOut();
	return 0;
}

int RTPPacket::PacketOut() {
	///theApp.Message(0,"payload		%02x\n"
	//		"seqNum			%04x\n"
	//		"timestamp		%08lx\n"
	//		"SSRC			%08lx\n"
	//		,GetPayLoadType(),GetSeqNum(),
	//		 GetTimeStamp()	,GetSSRC() );

	///theApp.Message(0,"payLoad: \n");
	CString str;
	for(int i = 0; i < 32; i++ ) {
		str.AppendFormat( "%02x ",payload[ i ] );
		if( i%16 == 15 ) str.AppendFormat( "\n" );
	}
	///theApp.Message(0,str);
	return 0;
}



# endif 







